<?
class item_list{

	private $items;
	private $itemsPerPage;
	private $pages;
	private $currentPage;
	private $editUrl;
	private $deleteUrl;
	private $html;
	private $pageUrl;
	
	public function __construct(array $iItems, $iItemsPerPage){
	
		$this -> items = $iItems;
		$this -> html = new view_htmlTag('ul');
		$this -> html -> setAttribute('class', 'item_edit_list');
		$this -> itemsPerPage = $iItemsPerPage;
		
		if(is_array($this -> items) && count($this -> items) > 0){
	
			$itemsAmount = count($this -> items);
			
			$this -> pages = ceil($itemsAmount / $this -> itemsPerPage);
			
		
		}
	
	}
	
	
	
	public function setPageUrl($iUrl){
	
		$this -> pageUrl = $iUrl;
	
	}
	
	public function isEmpty(){
	
		return count($this -> items) == 0 ? true : false;
	
	}
	
	public function setEditUrl($iUrl){
	
		$this -> editUrl = $iUrl;
	
	}
	
	public function setDeleteUrl($iUrl){
	
		$this -> deleteUrl = $iUrl;
	
	}
	
	
	public function renderPage($i){
	
		if($i < 0 || $i > $this -> pages - 1){
		
			$i = 0;
			
		
		}
		
		
		$this -> currentPage = $i;
		
		$startIndex = $i * $this -> itemsPerPage;
		$stopIndex = $startIndex + $this -> itemsPerPage;
			
			if($stopIndex >= count($this -> items)){
			
				$stopIndex = count($this -> items) - 1;
			
			}
			
			
			return $this -> renderNextPage($startIndex, $stopIndex);
	
	}
	
	
	
	private function renderNextPage($start, $stop){
		
		
		
		for($i = $start; $i <= $stop; $i++){
		
			$this -> html -> addChild($this -> renderRow($this -> items[$i]));
		
		}
		
		return $this -> html -> render();
	
	}
	
	public function getNextPage(){
		
		 if($this -> currentPage + 1 <= $this -> pages - 1){
			
		 	return $this -> pageUrl . '/' . ($this -> currentPage + 1);
		 
		 }else{
		 	return NULL;
		 }
	
	}
	
	
	public function getPrevPage(){
	
		 if($this -> currentPage - 1 >= 0){
			
		 	return $this -> pageUrl . '/' . ($this -> currentPage - 1);
		
		 
		 }else{
		 	return NULL;
		 }
	
	}
	
	private function renderRow($item){
	
		
		$row = new view_htmlTag('li');
		$links = new view_htmlTag('div');
		$text = new view_htmlTag('span');
		
		$editLink = new view_htmlTag('a');
		$deleteLink = new view_htmlTag('a');
		
		$editLink -> setAttribute('title', 'bewerken');
		$deleteLink -> setAttribute('title', 'verwijderen');

		$editLink -> setText('bewerken');
		$editLink -> setAttribute('href', $this -> editUrl . '/' . $item -> id);
		$editLink -> setAttribute('class', 'editLink');
		
		$deleteLink -> setText('verwijderen');
		$deleteLink -> setAttribute('href', $this -> deleteUrl . '/' . $item -> id);
		$deleteLink -> setAttribute('class', 'deleteLink');
		
		
		$text -> setText($item -> title);
		$links -> addChild($editLink);
		$links -> addChild($deleteLink);
		
		$row -> addChild($text);
		$row -> addChild($links);
		
		return $row;
		
		
		
	
	
	}






}




?>